package com.cll.prototype.captcha.controller;


import com.cll.prototype.common.captcha.service.IAuthCaptchaService;
import com.cll.prototype.common.dto.AjaxResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 验证码服务的表 前端控制器
 * </p>
 *
 * @author CLL
 * @since 2020-10-26
 */
@RestController
@RequestMapping("/captcha/auth_captcha")
public class AuthCaptchaController {

    private static final Logger logger = LoggerFactory.getLogger(AuthCaptchaController.class);

    @Autowired
    private IAuthCaptchaService authCaptchaService;

    /**
     * 1. 生成验证码
     * 2. 存储到数据库
     * 3. 调用扇出邮箱微服务发送
     * @param email 邮箱
     * @return  发送结果
     */
    @PostMapping(value = "/createRegisterCaptcha")
    public AjaxResult createRegisterCaptcha(@RequestParam("email") String email){
        logger.info("===>>> 验证码微服务接收到请求");
        try {
            return authCaptchaService.createRegisterCaptchaAndSendEmail(email);
        } catch (Exception e) {
            logger.error("****** 获取注册验证码抛出异常" + e.getMessage(), e);
            return AjaxResult.error(51, "获取注册验证码异常");
        }
    }

    /**
     * 校验验证码
     * @param email 邮箱号码
     * @param captcha   验证码
     * @return  检测结果
     */
    @PostMapping(value = "/checkCaptcha")
    public AjaxResult checkCaptcha(@RequestParam("email") String email,
                                   @RequestParam("captcha") String captcha){
        return authCaptchaService.checkCaptcha(email, captcha);
    }

}
